/**
 * 店铺业主管理API接口
 * 
 * @author Wu.Liang
 * @since 2025-01-30
 */

import api from './request'
import type { ApiResponse } from '@/types'

// 店铺业主查询参数接口
export interface ShopOwnerQueryParams {
  current?: number
  size?: number
  ownerName?: string
  ownerType?: number
  ownerPhone?: string
  ownerIdCard?: string
  companyName?: string
  email?: string
  status?: number
  keyword?: string
  createTimeStart?: string
  createTimeEnd?: string
}

// 店铺业主表单数据接口
export interface ShopOwnerFormData {
  id?: number
  ownerName: string
  ownerPhone: string
  ownerIdCard?: string
  ownerType: number
  companyName?: string
  companyLicense?: string
  contactAddress?: string
  email?: string
  status?: number
  remark?: string
}

// 店铺业主详情接口
export interface ShopOwnerDetail {
  id: number
  ownerName: string
  ownerPhone: string
  ownerIdCard?: string
  ownerType: number
  ownerTypeName: string
  companyName?: string
  companyLicense?: string
  contactAddress?: string
  email?: string
  status: number
  statusName: string
  remark?: string
  createTime: string
  updateTime: string
  createBy: number
  updateBy: number
  shopList?: ShopOwnerRelationVO[]
}

// 店铺业主关联VO接口
export interface ShopOwnerRelationVO {
  id: number
  shopId: number
  shopName: string
  ownerId: number
  ownerName: string
  ownerPhone: string
  ownerType: number
  ownerTypeName: string
  companyName?: string
  relationType: number
  relationTypeName: string
  ownershipRatio?: number
  leaseStartDate?: string
  leaseEndDate?: string
  rentPrice?: number
  depositAmount?: number
  contractNumber?: string
  contractFile?: string
  isPrimary: number
  status: number
  statusName: string
  remark?: string
  createTime: string
  updateTime: string
}

// 分页响应接口
export interface ShopOwnerPageResponse {
  records: ShopOwnerDetail[]
  total: number
  current: number
  size: number
}

/**
 * 店铺业主管理API
 */
export const shopOwnerApi = {
  /**
   * 分页查询店铺业主列表
   * @param params 查询参数
   * @returns 分页结果
   */
  getShopOwnerPage: (params: ShopOwnerQueryParams) => {
    return api.get<ApiResponse<ShopOwnerPageResponse>>('/api/region/shop-owner/page', { params })
  },

  /**
   * 根据ID获取店铺业主详情
   * @param id 业主ID
   * @returns 业主详情
   */
  getShopOwnerDetail: (id: number) => {
    return api.get<ApiResponse<ShopOwnerDetail>>(`/api/region/shop-owner/${id}`)
  },

  /**
   * 根据店铺ID获取业主列表
   * @param shopId 店铺ID
   * @returns 业主列表
   */
  getShopOwnersByShopId: (shopId: number) => {
    return api.get<ApiResponse<ShopOwnerDetail[]>>(`/api/region/shop-owner/shop/${shopId}`)
  },

  /**
   * 根据业主ID获取关联店铺列表
   * @param ownerId 业主ID
   * @returns 店铺列表
   */
  getShopsByOwnerId: (ownerId: number) => {
    return api.get<ApiResponse<ShopOwnerRelationVO[]>>(`/api/region/shop-owner/${ownerId}/shops`)
  },

  /**
   * 创建店铺业主
   * @param data 业主信息
   * @returns 操作结果
   */
  createShopOwner: (data: ShopOwnerFormData) => {
    return api.post<ApiResponse<number>>('/api/region/shop-owner', data)
  },

  /**
   * 更新店铺业主信息
   * @param id 业主ID
   * @param data 业主信息
   * @returns 操作结果
   */
  updateShopOwner: (id: number, data: Partial<ShopOwnerFormData>) => {
    return api.put<ApiResponse<boolean>>(`/api/region/shop-owner/${id}`, data)
  },

  /**
   * 删除店铺业主
   * @param id 业主ID
   * @returns 操作结果
   */
  deleteShopOwner: (id: number) => {
    return api.delete<ApiResponse<boolean>>(`/api/region/shop-owner/${id}`)
  },

  /**
   * 批量删除店铺业主
   * @param ids 业主ID列表
   * @returns 操作结果
   */
  batchDeleteShopOwners: (ids: number[]) => {
    return api.delete<ApiResponse<boolean>>('/api/region/shop-owner/batch', { data: ids })
  },

  /**
   * 启用店铺业主
   * @param id 业主ID
   * @returns 操作结果
   */
  enableShopOwner: (id: number) => {
    return api.put<ApiResponse<boolean>>(`/api/region/shop-owner/${id}/enable`)
  },

  /**
   * 禁用店铺业主
   * @param id 业主ID
   * @returns 操作结果
   */
  disableShopOwner: (id: number) => {
    return api.put<ApiResponse<boolean>>(`/api/region/shop-owner/${id}/disable`)
  },

  /**
   * 统计店铺业主数量
   * @param params 查询参数
   * @returns 统计结果
   */
  countShopOwners: (params: { ownerType?: number; status?: number }) => {
    return api.get<ApiResponse<number>>('/api/region/shop-owner/count', { params })
  },

  /**
   * 检查业主电话是否存在
   * @param ownerPhone 业主电话
   * @param excludeId 排除的业主ID
   * @returns 是否存在
   */
  checkOwnerPhone: (ownerPhone: string, excludeId?: number) => {
    return api.get<ApiResponse<boolean>>(`/api/region/shop-owner/exists/phone/${ownerPhone}`, {
      params: { excludeId }
    })
  },

  /**
   * 检查业主身份证号是否存在
   * @param ownerIdCard 业主身份证号
   * @param excludeId 排除的业主ID
   * @returns 是否存在
   */
  checkOwnerIdCard: (ownerIdCard: string, excludeId?: number) => {
    return api.get<ApiResponse<boolean>>(`/api/region/shop-owner/exists/idcard/${ownerIdCard}`, {
      params: { excludeId }
    })
  }
}
